/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import Common.Item;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author satkins
 */
public class DAO {

    private Statement stmt; 
    private Connection con;
    
    public DAO(){
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            con = DriverManager.getConnection("jdbc:mysql://localhost/digistore?" +
                    "user=root&password=admin");
            stmt = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Item> getItems(){
        ArrayList<Item> Items = new ArrayList<Item>();
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery("SELECT * FROM item");
        } catch (SQLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        int upc = 0;
        String itemName = null;
        String description = null;
        String make = null;
        String model = null;
        double price =0.0;
        
        try {
            while(rs.next())
            {
                upc = rs.getInt("upc");
                itemName = rs.getString("itemName");
                description = rs.getString("description");
                make = rs.getString("make");
                model = rs.getString("model");
                price = rs.getDouble("retailPrice");
                Item item = new Item(upc, itemName, description, make, model, price);
                Items.add(item);
            }
        } catch (SQLException ex) {
                Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        
        return Items;
    }

    public boolean CustomerLogIn(String phone, String password){
        ResultSet rs;
        try {
            PreparedStatement ps = con.prepareStatement("SELECT * FROM customer WHERE phone = ?");
            
            ps.setString(1, phone);
            rs = ps.executeQuery();
            
            ResultSetMetaData rsmd = rs.getMetaData();

            int count = rsmd.getColumnCount();
            if (count == 0){
                System.out.println("rsmd fault");
                return false;
            }
            rs.beforeFirst();
            rs.next();
            String passwrd = rs.getString("pword");          

            
            if(passwrd.equals(password)){
                System.out.println("pasword successful");
                return true;
            }
            else{
                System.out.println("BOO");
                return false;
            }
        }
        catch (SQLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

}
